﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SalaryMS
{
    public partial class SalaryExportUI : Form
    {
        public SalaryExportUI()
        {
            InitializeComponent();
            String sql = "SELECT CmpId,CmpName FROM Company";
            DataTable dt = DBSession.ExecuteDataTable(sql, null);
            cmbCmp_S.DataSource = dt;
            cmbCmp_S.DisplayMember = "CmpName";
            cmbCmp_S.ValueMember = "CmpId";
            if (cmbCmp_S.SelectedValue != null && cmbCmp_S.SelectedValue.ToString().Length > 0)
            {
                sql = "SELECT SalaryDate FROM SalaryDate WHERE CmpId='" + cmbCmp_S.SelectedValue.ToString() + "'";
            }
            else
            {
                sql = "SELECT SalaryDate FROM SalaryDate";
            }
            DataTable dtSD = DBSession.ExecuteDataTable(sql, null);
            cmbSalaryDate_S.DataSource = dtSD;
            cmbSalaryDate_S.DisplayMember = "SalaryDate";
            cmbSalaryDate_S.ValueMember = "SalaryDate";
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            String CmpId = (cmbCmp_S.SelectedValue!=null)?cmbCmp_S.SelectedValue.ToString():"";
            String SalaryDate = cmbSalaryDate_S.SelectedValue!=null?cmbSalaryDate_S.SelectedValue.ToString():"";
            String Path = txtPach.Text.Trim();
            if (CmpId.Length > 0 && SalaryDate.Length > 0 && Path.Length > 0)
            {
                String FileName = Path.TrimEnd('\\').TrimEnd('/') + '\\' + CmpId + "_" + SalaryDate + ".txt";
                if (!File.Exists(FileName))
                {
                    using (StreamWriter sw = File.CreateText(FileName))
                    {
                        //sw.WriteLine("Hello");
                        String sql = "SELECT BankAccount,Money,EmpName FROM Salary WHERE SalaryDate='"+SalaryDate+"' AND CmpId='"+CmpId+"'";
                        DataTable dt = DBSession.ExecuteDataTable(sql, null);
                        foreach (DataRow dr in dt.Rows)
                        {
                            sw.WriteLine(dr["BankAccount"].ToString()+" "+dr["Money"].ToString()+" "+dr["EmpName"].ToString());
                        }
                    }
                }

                MessageBox.Show("导出数据成功，文件保存在："+FileName);

            }
            else
            {
                MessageBox.Show("企业名称，薪资月份，导出路径均不能为空！");
            }
        }

        private void cmbCmp_S_SelectedValueChanged(object sender, EventArgs e)
        {
            String sql = "";
            if (cmbCmp_S.SelectedValue != null && cmbCmp_S.SelectedValue.ToString().Length > 0)
            {
                sql = "SELECT SalaryDate FROM SalaryDate WHERE CmpId='" + cmbCmp_S.SelectedValue.ToString() + "'";
            }
            else
            {
                sql = "SELECT SalaryDate FROM SalaryDate";
            }
            DataTable dtSD = DBSession.ExecuteDataTable(sql, null);
            cmbSalaryDate_S.DataSource = dtSD;
            cmbSalaryDate_S.DisplayMember = "SalaryDate";
            cmbSalaryDate_S.ValueMember = "SalaryDate";
        }
    }
}
